#include "Kinect.h"
#include "MouseMessage.h"

MouseMessage* Ogre::Singleton<MouseMessage>::ms_Singleton = 0;
MouseMessage g_singleTonMouseMessage;

MouseMessage::MouseMessage()
{
	for(int i = 0; i < MAX_HAND_NUM; i++)
	{
		lastLocation[i].x = 0;
		lastLocation[i].y = 0;
	}
}


void MouseMessage::updateMouse(myPoint location,int userID,int flag)
{
	int UID = userID;
	if(lastLocation[UID].x==0 && lastLocation[UID].y==0)
	{
		lastLocation[UID].x=location.x;
		lastLocation[UID].y=location.y;
	}

	if(flag == MOUSE_CLICK)
	{
		mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
		mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
		//mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
	}
	else if(flag == MOUSE_MOVE)
	{
		int firstHandId=-1;
		for(int i=0;i<MAX_HAND_NUM;i++)
		{
			if(Kinect::getSingleton().isHandTracking[i] != 0)
			{
				if(firstHandId == -1)
				{
					firstHandId = i;
					break;
				}
			}
		}
		//if(abs(location.x-lastLocation[UID].x)<5)
		//{
		//	location.x = lastLocation[UID].x;
		//}
		//if(abs(location.y-lastLocation[UID].y)<5)
		//{
		//	location.y = lastLocation[UID].y;
		//}

		if(UID == firstHandId)
		{
			//std::cout<<location.x<<","<<location.y<<std::endl;
			//mouse_event(MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE,
			//	-(location.x-160)*65536/1366*2,-(location.y-120)*65536/768*2,0,0);	
			mouse_event(MOUSEEVENTF_MOVE,
				(location.x-lastLocation[UID].x)*5, -(location.y-lastLocation[UID].y)*5, 0, 0);	
		}		
	}
	lastLocation[UID].x=location.x;
	lastLocation[UID].y=location.y;
}